package com.exp.practice.workbench.controller.student;

import com.exp.practice.base.exception.MyException;
import com.exp.practice.base.vo.PageVo;
import com.exp.practice.base.vo.ResultVo;
import com.exp.practice.workbench.domain.*;
import com.exp.practice.workbench.service.student.StudentPracticeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/workbench/student/practice")
public class StudentPracticeController {

    @Resource
    private StudentPracticeService studentPracticeService;

    //获得试卷列表
    @RequestMapping("/getExaminationList.do")
    @ResponseBody
    public PageVo getExaminationList(String pageNo, String pageSize,String stuId,String finished)  {
        PageVo pageVo=new PageVo();
        int total=studentPracticeService.getTotal(stuId,finished);
        List<Examination> list=studentPracticeService.getExaminationList(pageNo,pageSize,stuId,finished);
        pageVo.setTotal(total);
        pageVo.setDataList(list);
        return pageVo;
    }

    //获得试卷详情
    @RequestMapping("/doTest.do")
    public ModelAndView doTest(String eid, HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<>();
        map=studentPracticeService.getConcreteExamination(eid);
        modelAndView.addObject("examination",map.get("examination"));
        modelAndView.addObject("questionList",map.get("questionList"));
        session.setAttribute("questionList",map.get("questionList"));
        modelAndView.setViewName("workbench/student/practice/doTest");
        return modelAndView;
    }

    //评分
    @RequestMapping("/markTest.do")
    @ResponseBody
    public String markTest(String stuId,String eid, HttpServletRequest request,HttpSession session){
           int score=0;
           //获得题目列表
           List<Question> qList= (List<Question>) session.getAttribute("questionList");
        try {
            //获得分数
            for (int i = 0; i < qList.size(); i++) {
                 Question question=qList.get(i);
                 String rightAnswer=question.getRight_answer();
                 String stuAnswer=request.getParameter("answer_"+question.getQid());
                 if(rightAnswer.equals(stuAnswer)){
                     score+=10;
                 }
            }
            //插入一条答题情况记录
            ExaminationSituation examinationSituation=studentPracticeService.markTest(stuId,eid,score);
            String situationId=examinationSituation.getId();
            //插入多条具体答题情况记录
            for (int i = 0; i < qList.size(); i++) {
                Question question=qList.get(i);
                String qid=question.getQid();
                String rightAnswer=question.getRight_answer();
                String stuAnswer=request.getParameter("answer_"+question.getQid());
                String isRight=rightAnswer.equals(stuAnswer)?"Y":"N";
                ConcreteExaminationSituation concreteExaminationSituation=new ConcreteExaminationSituation();
                concreteExaminationSituation.setQid(qid);
                concreteExaminationSituation.setStuAnswer(stuAnswer);
                concreteExaminationSituation.setRightAnswer(rightAnswer);
                concreteExaminationSituation.setSituationId(situationId);
                concreteExaminationSituation.setIsRight(isRight);
                studentPracticeService.insertConcreteExaminationSituation(concreteExaminationSituation);
            }
            //将试卷的未完成状态置为完成
            studentPracticeService.setFinishedStatus(eid,stuId);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return "{\"score\":"+score+"}";
    }


    @RequestMapping("/detail.do")
    public ModelAndView detail(String eid,String stuId,HttpSession session){
        ModelAndView modelAndView=new ModelAndView();
        //获得试卷详情和题目详情
        modelAndView=doTest(eid,session);
        //获得该试卷完成情况
        ExaminationSituation examinationSituation=studentPracticeService.getExaminationSituation(eid,stuId);
        modelAndView.addObject("examinationSituation",examinationSituation);
        modelAndView.setViewName("workbench/student/practice/testDetail");
        return modelAndView;
    }

    @RequestMapping("/getConcreteExaminationSituation.do")
    @ResponseBody
    public ResultVo getConcreteExaminationSituation(String situationId){
        ResultVo resultVo=new ResultVo();
        //获得试卷中题目的答题情况
        List<ConcreteExaminationSituation> concreteExaminationSituationList=studentPracticeService.getBySituationId(situationId);
        resultVo.setDataList(concreteExaminationSituationList);
        resultVo.setSuccess(true);
        return resultVo;
    }
}
